﻿using System;
using System.Windows;
using System.Windows.Controls;
using DataStore.Domain;
using Infrastructure.Managers;
using Infrastructure.ViewModels;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace UI.Controls
{
    /// <summary>
    /// Interaction logic for AddCover.xaml
    /// </summary>
    public partial class AddCover : UserControl
    {
        public event EventHandler<CoverAddedEventArgs> CoverAdded;
        public void OnCoverAdded(object sender, CoverAddedEventArgs e)
        {
            if(CoverAdded != null)
            {
                CoverAdded(sender, e);
            }
        }

        CoverViewModel _cover;

        public CoverType Type
        {
            get
            {
                return _cover.Type;
            }
            set
            {
                _cover.Type = value;
            }
        }


        /// <summary>
        /// Initializes a new instance of the <see cref="AddCover"/> class.
        /// </summary>
        public AddCover()
        {
            InitializeComponent();

            _cover = new CoverViewModel(new Cover());
            DataContext = _cover;
        }

        /// <summary>
        /// Handles the Click event of the AddCover control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void AddCover_Click(object sender, RoutedEventArgs e)
        {
            CoverManager manager = new CoverManager();
            var newFilePath = manager.MoveFile(_cover.Path.Uri);
            _cover.Path.Uri = new Uri(newFilePath);

            OnCoverAdded(this, new CoverAddedEventArgs(_cover));

            _cover = new CoverViewModel(new Cover());
            DataContext = _cover;

            


        }

        /// <summary>
        /// Handles the Click event of the FileSelector control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void FileSelector_Click(object sender, RoutedEventArgs e)
        {
            CommonOpenFileDialog dialog = new CommonOpenFileDialog();
            dialog.Filters.Add(new CommonFileDialogFilter("All images", "*.bmp;*.jpg;*.jpeg;*.wmp;*.png;*.tif;*.tif;*.gif"));
            dialog.Filters.Add(new CommonFileDialogFilter("Bitmap", "*.bmp"));
            dialog.Filters.Add(new CommonFileDialogFilter("GIF", "*.gif"));
            dialog.Filters.Add(new CommonFileDialogFilter("Jpeg", "*.jpg;*.jpeg"));
            dialog.Filters.Add(new CommonFileDialogFilter("PNG", "*.png"));
            dialog.Filters.Add(new CommonFileDialogFilter("TIFF", "*.tif;*.tiff"));
            dialog.Filters.Add(new CommonFileDialogFilter("Windows Media Photo", "wmp"));
            dialog.EnsureFileExists = true;
            if(dialog.ShowDialog() == CommonFileDialogResult.OK)
            {
                // Set path
                LinkViewModel path;

                if (_cover.Path == null || _cover.Path.Link == null)
                {
                    
                    path = new LinkViewModel(new Link());
                }
                else
                {
                    path = _cover.Path;
                }

                path.Uri = new Uri(dialog.FileName);
                path.Location = LinkType.Local;

                if (_cover.Path != null &&_cover.Path.Link == null)
                {
                    _cover.Path = path;
                }
 

            }
        }

        /// <summary>
        /// Handles the ValueChanged event of the Size control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
#pragma warning disable 1574
        /// <param name="e">The <see cref="System.Windows.RoutedPropertyChangedEventArgs&lt;System.Double&gt;"/> instance containing the event data.</param>
#pragma warning restore 1574
        private void Size_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (_cover != null)
            {
                if (Size.Value == 1)
                {
                    _cover.Size = CoverSize.Small;
                }
                else if (Size.Value == 2)
                {
                    _cover.Size = CoverSize.Medium;
                }
                else if (Size.Value == 3)
                {
                    _cover.Size = CoverSize.Large;
                }
            }
        }
    }

    /// <summary>
    /// Event argument containing Cover data
    /// </summary>
    public class CoverAddedEventArgs : EventArgs
    {
        readonly CoverViewModel _cover;
        /// <summary>
        /// Gets the cover viewmodel.
        /// </summary>
        /// <value>The cover viewmodel.</value>
        public CoverViewModel CoverVM
        {
            get
            {
                return _cover;
            }
        }

        /// <summary>
        /// Gets the cover.
        /// </summary>
        /// <value>The cover.</value>
        public Cover Cover
        {
            get
            {
                return _cover.Cover;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CoverAddedEventArgs"/> class.
        /// </summary>
        /// <param name="cover">The cover.</param>
        public CoverAddedEventArgs(CoverViewModel cover)
        {
            _cover = cover;
        }


    }
}
